package ru.incoremedia.gaeutils.model;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.io.Serializable;


public abstract class BaseEntity {

    private Entity entity;

    public BaseEntity(String key) {
        entity = new Entity(this.getClass().getName(), key);
    }

    public BaseEntity(long key) {
        entity = new Entity(KeyFactory.createKey(this.getClass().getName(), key));
    }

    //db generated key
    public BaseEntity() {
        entity = new Entity(this.getClass().getName());
    }
    
    public BaseEntity(String key, Key parentKey) {
        entity = new Entity(this.getClass().getName(), key, parentKey);
    }

    public BaseEntity(long key, Key parentKey) {
        entity = new Entity(KeyFactory.createKey(parentKey, this.getClass().getName(), key));
    }

    //db generated key
    public BaseEntity(Key parentKey) {
        entity = new Entity(this.getClass().getName(), parentKey);
    }

    public BaseEntity(Entity entity) {
        this.entity = entity;
    }

    public Serializable getProperty(Enum property) {
        return (Serializable)entity.getProperty(property.toString());
    }

    public void setProperty(Enum property, Serializable value) {
        entity.setProperty(property.toString(), value);
    }

    public boolean hasProperty(Enum property) {
        return entity.hasProperty(property.toString());
    }

    public abstract Enum[] getPropertiesEnum();

    public Entity getEntity() {
        return entity;
    }

    public void setEntity(Entity entity) {
        this.entity = entity;
    }
}
